import QtQuick
import QtQuick.Controls
Item {
    property bool addMountCollection: false
    property var kmodel
    property ListModel klistmodel
    property int kmodelindex
    implicitWidth: 100
    implicitHeight:parent.height
    anchors {
        right: parent.right
        verticalCenter: parent.verticalCenter
        // margins: 3
    }
    // Component.onCompleted: {
    //     console.log(kmodelindex)
    //     // kmodel.type="123456789"
    // }
    Row {
        anchors.fill: parent
        spacing: 7
        Rectangle {
            width: 22; height: 22
            // color: "red"
            radius: 3
            anchors.verticalCenter: parent.verticalCenter
            Component{
                id:koperationbutton_kaddpage_comp
                // property string tabbarname:"创建"
                KAddPage{
                    id:koperationbutton_kaddpage
                }
            }
            Component{
                id:kkoperationbutton_kchildrect_comp
                KChildRect{

                }
            }
            Image{
                anchors.fill: parent
                source: "qrc:/ICON/icon/APPEND2.png"
                fillMode:Image.PreserveAspectCrop
                smooth: true
                mipmap: true
            ListView{
                anchors.fill: parent
                model:klistmodel
                MouseArea{

                    anchors.fill: parent
                    onClicked: {
                        if(kmodel.type==="Version"||kmodel.type==="MainExplanation"){
                            console.log(klistmodel.get(kmodelindex)["name"])
                            // console.log(klistmodel.get(kmodelindex).content)
                            var property_versionOrMainExplanation={
                                // kchildRect_listmodel_parent:klistmodel[kmodelindex],
                                kchildRect_listmodel:klistmodel.get(kmodelindex).content
                            }
                            // klistmodel.get(kmodelindex).content.get(0).childRect.get(0).xbegin=0.1
                            var addPageChildRect=kkoperationbutton_kchildrect_comp.createObject(parent,property_versionOrMainExplanation)
                            main_ktabbarview.appendPage2(addPageChildRect,kmodel.name)

                        }else{
                            var addPage
                            if(!addMountCollection){
                                console.log(kmodel.type)
                                if(kmodel.type==="mount"){
                                    // console.log(1)
                                    var properties_mount = {
                                        type: kmodel.type,
                                        names: kmodel.name,
                                        tabbarname: kmodel.name,
                                        listmodel:mount
                                    };
                                    addPage=koperationbutton_kaddpage_comp.createObject(parent,properties_mount)
                                    // addPage.tabbarname=kmodel.name
                                    // addPage.item.listmodel=mount
                                }else if(kmodel.type==="VersionControl"){
                                    // console.log(2)
                                    var properties_VersionControl = {
                                        type: kmodel.type,
                                        names: kmodel.name,
                                        tabbarname: kmodel.name,
                                        listmodel:VersionControl
                                    };
                                    addPage=koperationbutton_kaddpage_comp.createObject(parent,properties_VersionControl)
                                    // addPage.tabbarname=kmodel.name
                                }else if(kmodel.type==="MountCollection"){
                                    var properties_MountCollection = {
                                        type:kmodel.type,
                                        names: kmodel.name,
                                        tabbarname: kmodel.name,
                                        listmodel:mount
                                    };
                                    addPage=koperationbutton_kaddpage_comp.createObject(parent,properties_MountCollection)

                                }
                                main_ktabbarview.appendPage2(addPage,kmodel.name);
                            }else{// 添加mountcollection




                            }



                        }



                    }

                }
            }
        }
        }
        Rectangle {
            anchors.verticalCenter: parent.verticalCenter

            width: 22; height: 22
            // color: "red"
            radius: 3
            Image{
                anchors.fill: parent
                source: "qrc:/ICON/icon/EDIT.png"
                fillMode:Image.PreserveAspectCrop
                smooth: true
                mipmap: true
                KEditPage {
                    id: koperationbutton_editpage
                } // 确保 KEditPage 是可视元素（如 Item、Rectangle 等）

                Connections {
                    target: koperationbutton_editpage // 确保这里引用的是 editPage
                    function onEditfun(name) { // 改为 onEditfun
                        // console.log("Received signal from editPage with parameter:");
                        // 在这里处理外部逻辑
                        kmodel.name=name

                    }
                    function onEditfun_versioncontrol(main,minor) { // 改为 onEditfun
                        // console.log("Received signal from editPage with parameter:");
                        // 在这里处理外部逻辑
                        kmodel.MainVersion=Number(main)
                        kmodel.MinorVersion=Number(minor)

                    }
                    function onEditfun_version(revision) { // 改为 onEditfun
                        // console.log("Received signal from editPage with parameter:");
                        // 在这里处理外部逻辑
                        kmodel.RevisionVersion=Number(revision)

                    }

                }

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        var editPage = koperationbutton_editpage;
                        editPage.tabbarname=kmodel.name
                        // console.log(kmodel.type==="VersionControl")
                        // console.log(kmodel.type==="Version")
                        editPage.names=kmodel.name
                        if(kmodel.type==="VersionControl"){
                            editPage.mains=kmodel.MainVersion
                            editPage.minors=kmodel.MinorVersion
                        }
                        if(kmodel.type==="Version"){
                            editPage.mains=kmodel.MainVersion
                            editPage.minors=kmodel.MinorVersion
                            editPage.revisions=kmodel.RevisionVersion
                        }
                        editPage.isVisibleOfVersionControl=(kmodel.type==="VersionControl")
                        editPage.isVisibleOfVersion=(kmodel.type==="Version")
                        main_ktabbarview.appendPage(editPage);

                    }

                }

            }


        }
        Rectangle {
            width: 22; height: 22
            // color: "red"
            radius: 3
            anchors.verticalCenter: parent.verticalCenter
            Image{
                anchors.fill: parent
                source: "qrc:/ICON/icon/CLOSE.png"
                fillMode:Image.PreserveAspectCrop
                smooth: true
                mipmap: true


                Component{
                    id:compDeleted
                    Rectangle{
                        Column{
                            spacing:5
                            anchors.fill: parent
                            Rectangle{
                                width: parent.width
                                height: parent.height*0.3
                                color: "Red"

                            }

                            Rectangle{
                                width: parent.width
                                height: parent.height*0.3
                                Text{
                                    anchors.fill: parent
                                    text:"确定要删除"+kmodel.name+"-"+kmodel.type+"吗？"
                                }

                            }


                            Row{
                                spacing:5
                                width: parent.width
                                height: parent.height*0.3
                                Button{
                                    width: parent.width/2-5
                                    height: parent.height
                                    text:"确 定"
                                    onClicked: {
                                        // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                        // kmodel=null
                                        // listModelToObject(kCodeNestTreeView_ListModel)//保存
                                        // 查找当前项在模型中的索引

                                        // kmodel=Null
                                        // klistmodel.clear()
                                        klistmodel.remove(kmodelindex)
                                        listModelToObject(kCodeNestTreeView_ListModel)//保存
                                        main_kpopup.close();
                                    }
                                }
                                Button{
                                    width: parent.width/2-5
                                    height: parent.height
                                    text:"取 消"
                                    onClicked: main_kpopup.close();
                                }


                            }

                        }

                    }
                }

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        // console.log(123)
                        main_kpopup.comp=compDeleted
                        main_kpopup.open()
                    }
                }

            }

        }
    }




}
